const db = require('../db/index')
const bcrypt = require('bcrypt')
//导入crypto库生成uuid
const crypto = require('crypto')
const { date, object } = require('joi')

//导入fs处理文件
fs = require('fs')


// 获取全部管理员信息 根据参数类型来判断是哪类管理员
exports.getAllUser = (req, res) => {
    const query = req.body
    const number = (query.page - 1) * 10 //拿到offset的值
    const sql =
        `select user.name,user.id, user.departmentId,user.email,user.account from user_manager mg left join users user  on mg.userid = user.id   where mg.typeof_manager = ? order by user.id LIMIT 10 OFFSET ${number}  `
    db.query(sql, query.type, (err, results) => {
        if (err) return res.cc(err)
        res.send(results)
    })
}

//获取对应管理员的一个数组长度
exports.getAdminListLength = (req, res) => {
    const sql = 'select * from user_manager where typeof_manager = ? '
    db.query(sql, req.body.type, (err, result) => {
        if (err) return res.cc(err)
        res.send({
            length: result.length
        })
    })
}

//获取所有用户账号列表

exports.getAllAccount = (req, res) => {
    const query = req.body
    const sql = 'select user.account,user.id,user.name from  users user   '
    db.query(sql, query.type, (err, results) => {
        if (err) return res.cc(err)
        res.send(results)
    })

}


//设置员工为管理员
exports.setManagerbyemployee = (req, res) => {
    const query = req.body
    findUserIsEmpty(query.userid, query.typeof_manager, results => {
        if (results > 0) {
            res.send({
                status: 1,
                message: '此用户已经是管理员了'
            })
        } else {
            const sql = 'insert into user_manager set username=?,userid=?,typeof_manager =?'
            db.query(sql, [query.username, query.userid, query.typeof_manager], (err, results) => {
                if (err) return res.cc(err)
                res.send({
                    status: 0,
                    message: '添加管理员成功'
                })
            })
        }
    })

}

//删除用户管理员权限
exports.deleteUserManager = (req, res) => {
    const query = req.body
    findUserIsEmpty(query.userid, query.typeof_manager, results => {
        if (results > 0) {
            const sql = 'delete from  user_manager where userid=? and typeof_manager =?'
            db.query(sql, [query.userid, query.typeof_manager], (err, results) => {
                if (err) return res.cc(err)
                res.send({
                    status: 0,
                    message: '移除管理员权限成功'
                })
            })
        } else {
            res.send({
                status: 1,
                message: '此用户不是管理员,请重新确认'
            })

        }
    })

}

// 通过账号对用户搜索 account identity
exports.searchUser = (req, res) => {
    const { account, identity } = req.body
    const sql =
        'select user.name,user.id, user.department,user.email,user.account from user_manager mg left join users user  on mg.userid = user.id   where  user.account = ? and mg.typeof_manager = ?  '
    db.query(sql, [account, identity], (err, result) => {
        if (err) return res.cc(err)
        res.send(result)
    })
}


//封装一个判断userId是否存在的函数
const findUserIsEmpty = (id, type, callback) => {
    const selectsql = 'select * from user_manager where userid = ? and typeof_manager = ?'
    //校验id是否存在了
    db.query(selectsql, [id, type], (err, results) => {
        //query方法是异步调用，所以这里我们使用回调处理
        callback(results.length)
    })


}